package com.ljh.onlinetest.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.ExamPaperQuestionCustomerAnswer;
import com.ljh.onlinetest.domain.Subject;
import com.ljh.onlinetest.domain.TextContent;
import com.ljh.onlinetest.domain.question.QuestionObject;
import com.ljh.onlinetest.service.ExamPaperQuestionCustomerAnswerService;
import com.ljh.onlinetest.service.QuestionService;
import com.ljh.onlinetest.service.SubjectService;
import com.ljh.onlinetest.service.TextContentService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.HtmlUtil;
import com.ljh.onlinetest.utils.JsonUtil;
import com.ljh.onlinetest.utils.PageInfoHelper;
import com.ljh.onlinetest.viewmodel.admin.question.QuestionEditRequestVM;
import com.ljh.onlinetest.viewmodel.student.exam.ExamPaperSubmitItemVM;
import com.ljh.onlinetest.viewmodel.student.question.answer.QuestionAnswerVM;
import com.ljh.onlinetest.viewmodel.student.question.answer.QuestionPageStudentRequestVM;
import com.ljh.onlinetest.viewmodel.student.question.answer.QuestionPageStudentResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ljh
 * @ClassName QuestionAnswerController.java
 * @createTime 2022年03月23日
 */
@RestController("StudentQuestionAnswerController")
@RequestMapping("/api/student/question/answer")
public class QuestionAnswerController extends BaseApiController {
    private final ExamPaperQuestionCustomerAnswerService customerAnswerService;
    private final QuestionService questionService;
    private final TextContentService textContentService;
    private final SubjectService subjectService;

    @Autowired
    public QuestionAnswerController(ExamPaperQuestionCustomerAnswerService customerAnswerService, QuestionService questionService, TextContentService textContentService, SubjectService subjectService) {
        this.customerAnswerService = customerAnswerService;
        this.questionService = questionService;
        this.textContentService = textContentService;
        this.subjectService = subjectService;
    }

    /**
     * 错题本查询
     * @param model
     * @return
     */
    @PostMapping("/page")
    public RestResponse<PageInfo<QuestionPageStudentResponseVM>> pageList(@RequestBody QuestionPageStudentRequestVM model){
        model.setCreateUser(getCurrentUser().getId());
        //1.查询数据库对应的实体信息
        PageInfo<ExamPaperQuestionCustomerAnswer> pageInfo = customerAnswerService.studentPage(model);
        //2.转换成viewModel返回给前端
        PageInfo<QuestionPageStudentResponseVM> vmPageInfo = PageInfoHelper.copyMap(pageInfo, q -> {
            QuestionPageStudentResponseVM vm = modelMapper.map(q, QuestionPageStudentResponseVM.class);
            Subject subject = subjectService.selectById(q.getSubjectId());
            TextContent textContent = textContentService.selectById(q.getTextContentId());
            QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class);
            String clearHtml = HtmlUtil.clear(questionObject.getTitleContent());
            vm.setShortTitle(clearHtml);
            vm.setSubjectName(subject.getName());
            vm.setCreateTime(DateTimeUtil.dateFormat(q.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(vmPageInfo);
    }

    @PostMapping("/select/{id}")
    public RestResponse<QuestionAnswerVM> select(@PathVariable Integer id){
        QuestionAnswerVM questionAnswerVM = new QuestionAnswerVM();
        //db存储的用户答案
        ExamPaperQuestionCustomerAnswer customerAnswer = customerAnswerService.selectById(id);
        //用户答案vm
        ExamPaperSubmitItemVM examPaperSubmitItemVM = customerAnswerService.examPaperQuestionCustomerAnswerToVM(customerAnswer);
        //问题vm
        QuestionEditRequestVM questionEditRequestVM = questionService.getQuestionEditRequestVM(customerAnswer.getQuestionId());
        questionAnswerVM.setQuestionAnswerVM(examPaperSubmitItemVM);
        questionAnswerVM.setQuestionVM(questionEditRequestVM);
        return RestResponse.ok(questionAnswerVM);
    }
}
